//
//  RevampNameController.m
//  JackDeve
//
//  Created by apple on 17/5/10.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "RevampNameController.h"

@interface RevampNameController ()
{
    RevampView *v;
    NSDictionary * _dic;
}
@end

@implementation RevampNameController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建一个添加按钮
    UIButton *navLeftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [navLeftBtn setBackgroundImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [navLeftBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navLeftBtn];
    v =[[RevampView alloc]initWithFrame:self.view.frame];
    self.view =v;
    self.title = @"修改用户名";
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [self aa];
}
-(void)aa{
    //去别的页面跳转所用的方法
     __weak typeof (self) weakSelf = self;
    v.presentBlock =^(BOOL p) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"不允许首字符为数字" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [weakSelf presentViewController:alertC animated:YES completion:nil];
    };
    
    
    
}
//返回方法
-(void)click
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)clickRightBtn {
    //首字母不能为数字
    NSString *regex = @"[0-9]+";
    
    NSPredicate*predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if (v.accountNameTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"用户名不能为空"];
        [SVProgressHUD dismissWithDelay:1.5];
    }
  
    
    else if ([predicate evaluateWithObject:v.accountNameTF.text]){
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"不允许首字符为数字" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [self presentViewController:alertC animated:YES completion:nil];
    }
   
    else {
        [SVProgressHUD show];
        RevampModel * model =[[RevampModel alloc]init];
        [[RevampModel showData]getDataAndBlock:v.accountNameTF.text];
        
        //回调数据
        model.block=^(NSDictionary * dic) {
            _dic =dic;
             [self LoginHttpFinish];
        };
    }
}

- (void)LoginHttpFinish
{
    if ([[_dic objectForKey:@"errmsg"] isEqualToString:@"need login"]) {
        // 系应该失败
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"请您登陆" ];
        [SVProgressHUD dismissWithDelay:1.5];
    }
    else if ([[_dic objectForKey:@"errmsg"] isEqualToString:@"成功修改昵称"]){
        // 取本地信息
      Data *model = [HomeUserDefault getUserDefault];
      model.user_name = v.accountNameTF.text;
       // 存储
        [HomeUserDefault userDefaultWithModel:model];
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"更换成功" ];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
    else{
        // 修改失败
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"修改失败" ];
        [SVProgressHUD dismissWithDelay:1.5];
    }
}
@end
